package com.yimi.yinhepda.net;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.MoveSelectCurrentSpaceAdapter;
import com.pda.ym.base.CommonViewHolder;
import com.yimi.yinhepda.callbacks.OnClickCallBack;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.entity.bean.CurrentSpaceEntity;
import com.yimi.yinhepda.entity.request.QuerySpaceRequest;
import com.yimi.yinhepda.entity.response.QueryPlaceDictResponse;
import com.yimi.yinhepda.entity.response.QueryPlaceResponse;
import com.pda.ym.utils.Constants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lj
 * Date: 2018/12/27
 * Description: 查询场地请求
 */
public class QueryPlaceNetRequest {

    /**
     * 部门
     */
    private String deptCode;
    private MoveSelectCurrentSpaceAdapter currentSpaceAdapter;
    private CurrentSpaceEntity currentSpaceEntity;
    private String title;

    /**
     * 查询数据字典，该分拨是否在数据字典中
     */
    public QueryPlaceNetRequest queryDataDict(final UICallBack callBack) {
        Map<String, String> params = new HashMap<>();
        //移库倒货数据字典字段
        params.put("params", "yiku_transfer_cargo_switch");
        new NetRequest().setMethod(NetRequest.Method.GET)
                .setUrl(Constants.URL_QUERY_DICT_VALUE)
                .setParams(params)
                .setConnTimeOut(5 * 1000)
                .setResponsClazz(QueryPlaceDictResponse.class)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        callBack.onError(msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        QueryPlaceDictResponse response = (QueryPlaceDictResponse) obj;
                        if (response.getData() != null) {
                            Map<String, String> switchData = response.getData().getMoveStockSwitch();
                            if (switchData != null) {
                                for (String key : switchData.keySet()) {
                                    String value = switchData.get(key);
                                    if (value.equals(deptCode)) {
                                        //分拨在数据字典中
                                        callBack.onSuccess(null);
                                        return;
                                    }
                                }
                            }
                        }
                        callBack.onError("当前部门未开启该功能！");
                    }
                });
        return this;
    }

    /**
     * 根据部门获取场地
     */
    public QueryPlaceNetRequest querySpaceByDept(final UICallBack callBack) {
        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setResponsClazz(QueryPlaceResponse.class)
                .setUrl(Constants.URL_QUERY_SPACE_BY_DEPT)
                .setRequestObject(new QuerySpaceRequest(deptCode))
                .setConnTimeOut(5 * 1000)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        callBack.onError(msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        QueryPlaceResponse response = (QueryPlaceResponse) obj;
                        callBack.onSuccess(response.getData());
                    }
                });
        return this;
    }

    /**
     * 显示选择场地对话框
     */
    public QueryPlaceNetRequest showSelectSpaceDialog(Context context, final List<CurrentSpaceEntity> dataList, final OnClickCallBack clickCallBack) {
        final LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.alert_dialog_select_space, null);
        TextView tvTitle = layout.findViewById(R.id.tv_title);
        if(title != null){
            tvTitle.setText(title);
        }
        RecyclerView rvDataList = layout.findViewById(R.id.rv_select_space);
        Button btnOK = layout.findViewById(R.id.btn_sure);
        Button btnBack = layout.findViewById(R.id.btn_back);
        final Dialog dialog = new Dialog(context, R.style.AsyncTaskDialog);
        dialog.addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.getWindow().getDecorView().setPadding(20, 20, 20, 20);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);

        currentSpaceAdapter = new MoveSelectCurrentSpaceAdapter(context, dataList, new CommonViewHolder.onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                currentSpaceEntity = dataList.get(position);
                if (currentSpaceEntity.isCheck()) {
                    currentSpaceEntity.setCheck(false);
                } else {
                    for (CurrentSpaceEntity item : dataList) {
                        if (item.equals(currentSpaceEntity)) {
                            item.setCheck(true);
                        } else {
                            item.setCheck(false);
                        }
                    }
                }
                currentSpaceAdapter.notifyDataSetChanged();
            }

            @Override
            public void onItemLongClickListener(int position) {
            }
        });

        rvDataList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvDataList.setAdapter(currentSpaceAdapter);

        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCallBack.onSure(currentSpaceEntity, dialog);
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                clickCallBack.onCancle();
            }
        });
        return this;
    }


    public String getDeptCode() {
        return deptCode;
    }

    public QueryPlaceNetRequest setDeptCode(String deptCode) {
        this.deptCode = deptCode;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public QueryPlaceNetRequest setTitle(String title) {
        this.title = title;
        return this;
    }
}
